﻿using System.Linq;
using Inscriptionem.Models.Blog;
using Inscriptionem.Models.Blog.Enums;

namespace Inscriptionem.ORM.Extensions
{
    public static class PostExtensions
    {
        public static Article ToArticle(this Post post)
        {
            var comments = post.Comments.Where(c => (c.IsPublished && !c.IsDeleted) || (Authorization.Member.Current.ID != 0 && (!c.IsDeleted && c.Member.ID == Authorization.Member.Current.ID)));

            var article = new Article()
            {
                ID = post.ID,
                Type = ArticleType.Post,
                Title = post.Title,
                Content = post.Content,
                IsCommentable = post.IsCommentable,
                IsSectionPinned = post.IsSectionPinned,
                IsCategoryPinned = post.IsCategoryPinned,
                ViewCount = post.ViewCount,
                CreatedDate = post.CreatedDate,
                CommentsCount = comments.Count(),
            };

            article.Categories.AddRange(post.PostCategories.Select(pc => pc.Category));

            return article;
        }
    }
}